﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class UpdateAchievementsCommand extends Command {

		[Inject]
        public var achievementsModel:AchievementModel;
		[Inject]
        public var userModel:UserModel;
        private var _popupOpened:Boolean;// = false

        override public function execute():void{
            var _local1:AchievementVO;
            var _local9:Array;
            var _local10:RewardVO;
            var _local11:String;
            var _local2:Array = this.achievementsModel.collectedRewardIDs;
            if (((_local2) && ((_local2.length > 0)))){
                _local9 = this.achievementsModel.getAllRewards();
                for each (_local10 in _local9) {
                    for each (_local11 in _local2) {
                        if (_local11 == _local10.id){
                            _local10.rewardCollected = true;
                        };
                    };
                };
                this.achievementsModel.collectedRewardIDs = [];
            };
            var _local3:int = this.userModel.numberOfCuredPets;
            _local1 = this.achievementsModel.getAchievementByID(AchievementModel.CURED_PETS);
            _local1.points = _local3;
            this.checkRewardsForCompleted(_local1);
            var _local4:int = this.userModel.numberOfCuredElders;
            _local1 = this.achievementsModel.getAchievementByID(AchievementModel.CURED_ELDERS);
            _local1.isLocked = (_local1.unlockLevel > this.userModel.level);
            _local1.points = _local4;
            this.checkRewardsForCompleted(_local1);
            var _local5:int = this.userModel.numberOfBirdsCollected;
            _local1 = this.achievementsModel.getAchievementByID(AchievementModel.COLLECTED_BIRDS);
            _local1.isLocked = (_local1.unlockLevel > this.userModel.level);
            _local1.points = _local5;
            this.checkRewardsForCompleted(_local1);
            var _local6:int = this.userModel.numberOfCollectedSymbols;
            _local1 = this.achievementsModel.getAchievementByID(AchievementModel.COLLECTED_GEMS);
            _local1.isLocked = (_local1.unlockLevel > this.userModel.level);
            _local1.points = _local6;
            this.checkRewardsForCompleted(_local1);
            var _local7:int = this.userModel.numberOfUsedEnergy;
            _local1 = this.achievementsModel.getAchievementByID(AchievementModel.USED_BOOSTS);
            _local1.isLocked = (_local1.unlockLevel > this.userModel.level);
            _local1.points = _local7;
            this.checkRewardsForCompleted(_local1);
            var _local8:int = this.userModel.numberOfFedDoctors;
            _local1 = this.achievementsModel.getAchievementByID(AchievementModel.SERVED_COFFEES);
            _local1.isLocked = (_local1.unlockLevel > this.userModel.level);
            _local1.points = _local8;
            this.checkRewardsForCompleted(_local1);
        }
        private function checkRewardsForCompleted(_arg1:AchievementVO):void{
            var _local2:RewardVO;
            var _local3:Boolean;
            for each (_local2 in _arg1.rewards) {
                if (!_local2.rewardCollected){
                    _local3 = _local2.completed;
                    _local2.completed = (_arg1.points >= _local2.pointsNeeded);
                    if (((((!(_local3)) && (_local2.completed))) && (!(this._popupOpened)))){
                        this._popupOpened = true;
                        dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.ACHIEVEMENT_REWARD_COMPLETED_POPUP, _local2));
                    };
                };
            };
        }

    }
}//package net.wooga.pets.controllers 
